package com.david4.jizhang.util;

import java.util.Date;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;


/**
 * 
 * @author <a href="mailto:hanxiaojun85@gmail.com">hanxiaojun</a>
 * @since 2010-5-19
 */
public class ResumeRefresh {
	private static String username = "ad1v1";
	private static String passwd = "111111";
	/**
	 * @param args
	 * @throws  
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		//Random random = new Random();
//		int begin = 0;
//		int end = 40;
//		while(true){
//			//refresh51job();
//			int id = (int)(begin+(end-begin)*Math.random());
//			System.out.println(id);
//			//Thread.sleep(1000*id);
//		}
	 
		refreshzhaopin1();
	}
	
	public static void refresh51job()throws Exception{
		 String loginURL = "http://my.51job.com/my/My_AjaxLogin.php?username=java4&userpwd=987654321&type=in";
		 HttpClient client = new HttpClient();
		 HttpMethod getmethod = new GetMethod(loginURL);
		 getmethod.setRequestHeader("X-Requested-With", "XMLHttpRequest");
		 getmethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6");
		 client.executeMethod(getmethod);
		 String content = Util.convertStreamToString(getmethod.getResponseBodyAsStream());
		 getmethod.releaseConnection();
		 System.out.println(content);
		 
		 String refreshURL = "http://my.51job.com/cv/CResume/RefreshResume.php?isJSON=1&rand=0.4570733235352359&jsoncallback=jsonp1274259202039&_=1274259210650&ReSumeID=57012622&isenglish=0";
		 HttpMethod refresh = new GetMethod(refreshURL);
		 refresh.setRequestHeader("X-Requested-With", "XMLHttpRequest");
		 refresh.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6");
		 client.executeMethod(refresh);
		 String content1 = Util.convertStreamToString(refresh.getResponseBodyAsStream());
		 refresh.releaseConnection();
		 System.out.println(content1);
		 
		 String conURL="http://my.51job.com/cv/CResume/RefreshResume.php?isJSON=1&Read=0&isenglish=0&rand=0.38600106855911487&jsoncallback=jsonp1274259202040&_=1274259228479&ReSumeID=57012622&MPNation=137&Mobile=18478024&HPNation=086&HPCity=%E5%8C%BA%E5%8F%B7&HPNumber=%E7%94%B5%E8%AF%9D%E5%8F%B7%E7%A0%81&FPNation=086&FPCity=%E5%8C%BA%E5%8F%B7&FPNumber=%E6%80%BB%E6%9C%BA%E5%8F%B7%E7%A0%81&FPExtension=%E5%88%86%E6%9C%BA&current_situation=0&EmailAdd=hanxiaojun85%40gmail.com&isAlert=1";
		 HttpMethod confirm = new GetMethod(conURL);
		 confirm.setRequestHeader("X-Requested-With", "XMLHttpRequest");
		 confirm.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6");
		 client.executeMethod(confirm);
		 
		 String content2 = Util.convertStreamToString(confirm.getResponseBodyAsStream());
		 confirm.releaseConnection();
		 System.out.println(content2);
	}
	
	public static void refreshzhaopin()throws Exception{
		String loginURL = "http://my.zhaopin.com/loginmgr/loginproc.asp?DYWE=1251992206073.428417.1274183063.1274278259.3";
		 HttpClient client = new HttpClient();
		 System.setProperty("apache.commons.httpclient.cookiespec", "RFC2109");
		 PostMethod login = new PostMethod(loginURL);
		 login.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		 login.setRequestHeader("Content-Length", "79");

		 login.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6");
		 NameValuePair params[] = { new NameValuePair("int_count", "999"),
					new NameValuePair("isautologin", "1"),
					new NameValuePair("loginname", "ad1v1@163.com"),
					//new NameValuePair("loginname", "ad1v1%40163.com"),
					new NameValuePair("password", "987654321"),
					//new NameValuePair("requestFrom", "portal"),
					new NameValuePair("bkurl", "")};
		 login.setRequestBody(params);
		 client.executeMethod(login);
		 String content = Util.convertStreamToString(login.getResponseBodyAsStream(),"UTF-8");
		 login.releaseConnection();
		 System.out.println(content);
		 
		 
		 String umloginURL = "http://my.zhaopin.com/loginmgr/umlogin.asp";
		 HttpMethod umlogin = new GetMethod(umloginURL);
		 umlogin.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6");
		 client.executeMethod(umlogin);
		 String content1 = Util.convertStreamToString(umlogin.getResponseBodyAsStream(),"UTF-8");
		 umlogin.releaseConnection();
		 System.out.println("ok======="+content1);
		 
		 String s = "http://my.zhaopin.com/MYZHAOPIN/resume_index.asp?t=83417.8&f=login";
		 HttpMethod index = new GetMethod(s);
		 index.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6");
		 client.executeMethod(index);
		 String content4 = Util.convertStreamToString(index.getResponseBodyAsStream(),"UTF-8");
		 index.releaseConnection();
		 System.out.println(content4);
		 
		 
//		 String refreshURL = "http://my.zhaopin.com/myzhaopin/resume_publish.asp?ext_id=JR034266018R90000001000&resume_id=125159163&Version_Number=1&language_id=1&DYWE=1251992206073.428417.1274183063.1274259935.3";
//		 HttpMethod refresh = new GetMethod(refreshURL);
//		 refresh.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6");
//		 client.executeMethod(refresh);
//		 String content2= Util.convertStreamToString(refresh.getResponseBodyAsStream(),"UTF-8");
//		 refresh.releaseConnection();
//		 System.out.println(content2);
	}
	public static void refreshzhaopin1()throws Exception{
		 HttpClient client = new HttpClient();
		 HttpState state = getHttpState();
		 client.setState(state);
		 String s = "http://my.zhaopin.com/MYZHAOPIN/resume_index.asp?t=83417.8&f=login";
		 HttpMethod index = new GetMethod(s);
		 index.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6");
		 client.executeMethod(index);
		 String content4 = Util.convertStreamToString(index.getResponseBodyAsStream(),"UTF-8");
		 index.releaseConnection();
		 System.out.println(content4);
	}
	public static HttpState getHttpState(){
		 HttpState initialState = new HttpState();  
		 String cookieStr = getCookie();
		 String [] cookies = cookieStr.split("; ");
		 for(int i=0;i<cookies.length;i++){
			 String cooStr = cookies[i];
			 String[] coo = cooStr.split("=");
						 Cookie cookie=new Cookie();  
			 cookie.setDomain(".zhaopin.com");  
			 cookie.setPath("/");  
			 cookie.setName(coo[0]);  
			 if(coo.length>1)
			 cookie.setValue(coo[1]);  
			 initialState.addCookie(cookie); 
		 }
		return initialState;
	}
	
	public static String getCookie(){
		return "__zpWAM=1251992206073.428417.1274183063.1274278259.3; __zpWAMs1=1; __zpWAMs2=1; firstchannelurl=http%3A//my.zhaopin.com/loginmgr/login.asp%3FBkUrl%3D%252FMYZHAOPIN%252Fperson%255Finfo%252Easp%253F; lastchannelurl=http%3A//my.zhaopin.com/loginmgr/loginproc.asp%3FDYWE%3D1251992206073.428417.1274183063.1274278259.3; dywea=95841923.1348882904436939800.1274277846.1274277846.1274277846.1; dyweb=95841923.22.10.1274277846; dywec=95841923; dywez=95841923.1274277846.1.1.dywecsr=(direct)|dyweccn=(direct)|dywecmd=(none); __utma=269921210.1795169956.1274277846.1274277846.1274277846.1; __utmb=269921210.22.10.1274277846; __utmc=269921210; __utmz=269921210.1274277846.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); JSloginnamecookieindex=ad1v1%40163%2Ecom; JSloginnamecookie=ad1v1%40163%2Ecom; llt103426601=5%2F19%2F2010+11%3A07%3A13+PM; myzl103426601=0%7C2010%2D5%2D18+19%3A48%3A25; monitorlogin=Y; strloginusertype=4; HTTPFDSIP=http%3A%2F%2F192%2E168%2E9%2E226; dywem=95841923.y; vimg%5Fcode=6045623571424E347A362; localipName=±±¾©; JSsUserInfo=342C713654365A611C350569026B532C03365F365B611D350D697A6B2F2C083608360F611D354069076B212C04365F36586102355569596B0C2C0E363C3625611135DFFE650DFA7D0E362736256111350D69746B252C08365236296162350B690D6B352C78365436506169357B690B6B092C54360736136145355769596B0B2C40360736536119357669516B0C2C54360036076102355569596B0C2C0E363A363F61113504690D6B202C613654365E61013507690F6B412C07365A36516114350D69726B252C083652362F6162350B690D6B2D2C7836543650616F3572690B6B512C0E3628362761113506690D6B342C7436543621617E350669056B552C07365F365D611C3507690E6B5A2C76362836566117357269776B5C2C0E362D36286111350D69656B282C083652363E6178350B69076B5A2C9; JSShowname=%E9%9F%A9%E6%99%93%E5%86%9B; JSisLogin=";
		}
}
